var app = getApp();
Page({
  data: {
    audioData: {
      dataUrl: "http://yinyueshiting.baidu.com/data2/music/0bb054cfccca36661cc0ed26513bd0a7/261813945/261813857147600128.mp3?xcode=065b19cd988ba93d180b9e6dd2675aac",
      title: '音频3'
    },
    //init 方法中设置
    //audioSeekStatus: true,
    // audioLength: 0,
    // audioNowNumber: 0,
    // audioMaxTimer: '0:0',
    // audioNowTimer: '0:0',
    // audioBtnText: '播放'
  },
  components: {
    test: {
      text: "my login btn text"
    }
  },
  onReady: function () {
    var that = this;
    this.audioInit();

    // var state = this.initJudge();
    // 设置全局定时器附带执行的函数
    app.changeAudioCallback(that.getAudioStatus);

    if( app.globalData.audioState.status == 1 ){
      //播放
    }else{
      //暂停 停止
      console.log(app.globalData.system)
      if(app.globalData.system){
        //安卓和其他
        this.playAudio();
      }else{
        //iOS
        wx.showModal({
          title:'点击播放',
          confirmText:'播放',
          success: function(event){
            if( event.confirm ){
              console.log('点击播放');
              that.playAudio();
            }
          },
        });
      };
    };
  },
  onUnload: function () {
    // 设置全局定时器附带执行的函数
    app.changeAudioCallback(function(){});
  },
  audioInit: function () {
    var that = this;
    this.setData({
      audioSeekStatus: true,
      audioLength:app.globalData.audioState.duration || that.data.audioData.audioLength,
      audioNowNumber: app.globalData.audioState.currentPosition || 0,
      audioMaxTimer: this.formNumberToTime(app.globalData.audioState.duration) || '0:0',
      audioNowTimer: this.formNumberToTime(app.globalData.audioState.currentPosition) || '0:0',
      audioBtnText: app.globalData.audioState.status == 1?'暂停':'播放'
    });
  },
  playAudio: function () {
    console.log(this.data.audioData);
    app.playAudio(this.data.audioData,function(){});
  },
  pauseAudio: function () {
    app.pauseAudio();
  },
  stopAudio: function () {
    app.stopAudio();
  },
  audioBtn: function () {
    app.playAndPause(this.data.audioData,function(){});
  },
  getAudioStatus: function(){
    var that = this;
    if( this.data.audioSeekStatus ){
      //slider max 如果设置为0可能导致 拖动点回不到圆点
      this.setData({
        audioLength:app.globalData.audioState.duration || that.data.audioData.audioLength,
        audioNowNumber: app.globalData.audioState.currentPosition || 0,
        audioMaxTimer: this.formNumberToTime(app.globalData.audioState.duration) || '0:0',
        audioNowTimer: this.formNumberToTime(app.globalData.audioState.currentPosition) || '0:0',
        audioBtnText: app.globalData.audioState.status == 1?'暂停':'播放'
      });
    }else{
      this.setData({
        audioLength:app.globalData.audioState.duration || that.data.audioData.audioLength,
        // audioNowNumber: app.globalData.audioState.currentPosition || 0,
        audioMaxTimer: this.formNumberToTime(app.globalData.audioState.duration) || '0:0',
        audioNowTimer: this.formNumberToTime(app.globalData.audioState.currentPosition) || '0:0',
        audioBtnText: app.globalData.audioState.status == 1?'暂停':'播放'
      });      
    };
  },
  changeSlider: function(event){
    app.seekAudio(event.detail.value);
  },
  startSlider: function(){
    this.setData({
      audioSeekStatus: false
    });
  },
  endSlider: function(){
    var that = this;
    setTimeout(function(){
      that.setData({
        audioSeekStatus: true
      });
    },500);
  },
  setAudioTitle: function(event){
    console.log(event.detail.value);
    this.setData({
      audioData:{
        dataUrl: this.data.audioData.dataUrl,
        title: event.detail.value
      }
    })
  },
  setAudioUrl: function(event){
    console.log(event.detail.value);
    this.setData({
      audioData:{
        dataUrl: event.detail.value,
        title: this.data.audioData.title
      }
    })
  },
  //时间格式转换
  formNumberToTime: function(value){
    if( !value && value == undefined && typeof value == 'undefined'){
      return false;
    };
    var number = Number(value);
    var minute = Math.floor(number/60);
    var second = Math.floor(number%60);
    var str = minute + ':' + second;
    return str;
  }
})
